const service = ajax.create({
  // baseURL: process.env.BASE_API, // api的base_url
  baseURL: getBaseURL(), // api的base_url
  timeout: Config.timeout // 请求超时时间
})

export function request(config) {
  const { url, method, params, data } = config;
  
  uni.request({
    url: url,
    method: method,
    header: {
      token: uni.getStorageSync('token')
    },
    data: method.toLowerCase() === 'get' ? params : data,
    success: function(resp) {
      if (resp.statusCode == 401) {
        uni.redirectTo({
          url: '../user/login'
        });
      } else if (resp.statusCode == 200 && resp.data.code == 200) {
        let data = resp.data;
        if (data.hasOwnProperty("token")) {
          console.log(resp.data);
          let token = data.token;
          uni.setStorageSync("token", token);
        }
        config.success && config.success(resp);
      } else {
        uni.showToast({
          icon: 'none',
          title: resp.data
        });
      }
    }
  });
}